﻿using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace Rio.Framework.Pages.Helpers.Utils
{
    public class ColorUtils
    {
        public static SolidColorBrush GetBrushFromHexString(string aarrggbb)
        {
            String xamlString = "<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Background=\"" + aarrggbb + "\"/>";
            Canvas c = (Canvas)System.Windows.Markup.XamlReader.Load(xamlString);
            return (SolidColorBrush)c.Background;
        }

        public static Color GetColorFromString(string hexColor)
        {
            uint result;
            if (uint.TryParse(hexColor.TrimStart('#', '0'), System.Globalization.NumberStyles.HexNumber, null, out result))
            {
                const uint a = 0xFF;
                uint r = (result >> 16) & 0xFF;
                uint g = (result >> 8) & 0xFF;
                uint b = result & 0xFF;

                return Color.FromArgb((byte)a, (byte)r, (byte)g, (byte)b);
            }

            return Colors.Black;
        }
    }
}
